
import {
  getClassByGrade
} from '@/api/basic.js'
import {
  selectStuAttendlist
} from '@/api/attend.js'
import CourseChapter from '@/components/Course/CourseChapter/index.vue'
export default {
  components: {
    CourseChapter
  },
  data() {
    return {
      classDatas: [],
      studentParam: {
        // courseId: this.$route.query.courseId || null,
        courseCatalogId: this.$route.query.courseCatalogId || null,
        className: ''
      },
      studentDatas: [],
      statistics: {
        signin: 0, // 签到总数
        absence: 0 // 未到总数
      }
    }
  },
  created() {
    this.getDatas()
  },
  watch: {
    studentParam: {
      handler() {
        this.selectStuAttendlist()
      },
      deep: true
    },
    $route: {
      handler() {
        // this.studentParam.courseId = this.$route.query.courseId
        this.studentParam.courseCatalogId = this.$route.query.courseCatalogId
      },
      deep: true
    }
  },
  methods: {
    getDatas() {
      this.getClassByGrade()
      this.selectStuAttendlist()
    },
    getClassByGrade() {
      getClassByGrade({
        gradeId: this.$route.query.courseCatalogId || 0
      }).then(response => {
        if (response.code === 20000 || response.code === 200) {
          this.classDatas = response.data
        } else {
          this.$message({
            type: 'info',
            message: '班级获取失败'
          })
        }
      })
    },
    signinOpen() {
      if (this.$route.query.courseCatalogId) {
        this.centerDialogVisible = true
      } else {
        this.$alert('请先选择左侧课时再签到', '签到提示', {
          confirmButtonText: '确定'
        })
      }
    },
    selectStuAttendlist() {
      if (this.$route.query.courseCatalogId) {
        selectStuAttendlist(this.studentParam).then(response => {
          if (response.code === 20000 || response.code === 200) {
            // console.log(response.data)
            this.studentDatas = response.data
            this.statistics.signin = response.total
            this.statistics.absence = response.data.length - response.total
            // console.log(this.studentDatas)
          } else {
            this.$message({
              type: 'info',
              message: '学生获取失败'
            })
          }
        })
      }
    }
  }
}
